/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ComGLHoodOView.java
*
* Created on November 15, 2007, 7:51 PM
 */
package gui.genomemap.view.outputView;

import genomemap.data.OrderElement;
import genomemap.worker.ProgCurve;

import gui.genomemap.event.event.WorkerOutputEvent;
import gui.genomemap.outputModel.AnnealOModel;
import gui.genomemap.outputModel.ProgCurveOModel;
import gui.genomemap.popup.ProgCurveRTPanel;
import gui.genomemap.view.outputView.options.ProgCurveOOptions;

import javax.swing.JDialog;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author  stewari1
 */
public class ProgCurveOView extends OutputView {

    /** Field description */
    private JPopupMenu rtPopUp = null;

    /** Field description */
    private final ProgCurveOOptions optionsDialog = new ProgCurveOOptions(new javax.swing.JFrame(),
                                                        false);


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JTable bestOrders_JTable;

    /** Field description */
    private javax.swing.JScrollPane jScrollPane1;

    /** Field description */
    private javax.swing.JCheckBox rt_JCheckBox;

    /** Creates new form ComGLHoodOView */
    public ProgCurveOView() {

        initComponents();
        optionsDialog.setOutputView(this);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1      = new javax.swing.JScrollPane();
        bestOrders_JTable = new javax.swing.JTable();
        rt_JCheckBox      = new javax.swing.JCheckBox();

        bestOrders_JTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

            { null, null, null, null, null }, { null, null, null, null, null },
            { null, null, null, null, null }, { null, null, null, null, null }

        }, new String[] { "Segment #", "Starting Probe Order", "Best Probe Order", "Likelihood",
                          "Contig Count" }) {

            final Class[] types = new Class[] { java.lang.Integer.class, java.lang.String.class,
                    java.lang.String.class, java.lang.Double.class, java.lang.Object.class };
            final boolean[] canEdit = new boolean[] { false, false, false, false, false };
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }

        });
        jScrollPane1.setViewportView(bestOrders_JTable);
        rt_JCheckBox.setSelected(true);
        rt_JCheckBox.setText("Enable Runtime Control");
        rt_JCheckBox.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rt_JCheckBoxActionPerformed(evt);
            }

        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addComponent(rt_JCheckBox).addGap(
                    24, 24, 24).addComponent(
                    jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568,
                    Short.MAX_VALUE).addContainerGap()));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addGroup(
                    layout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 107,
                        javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                            rt_JCheckBox)).addContainerGap(39, Short.MAX_VALUE)));

    }    // </editor-fold>//GEN-END:initComponents

    /**
     * Method description
     *
     * @param evt description
     */
    private void rt_JCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_rt_JCheckBoxActionPerformed

        if (!rt_JCheckBox.isSelected()) {

            if (rtPopUp != null) {
                rtPopUp.setVisible(false);
            }

        } else {

            if (rtPopUp == null) {


                // first time job
                rtPopUp = ProgCurveRTPanel.createPopup(getView());


                // add popup menu listener to sync with the rt checkbox
                rtPopUp.addPopupMenuListener(new PopupMenuListener() {

                    public void popupMenuCanceled(PopupMenuEvent e) {}
                    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                        rt_JCheckBox.setSelected(false);
                    }
                    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}

                });
            }
        }

        rtPopUp.show(this, 100, 50);

    }    // GEN-LAST:event_rt_JCheckBoxActionPerformed

    public void refresh() {

        if (getOutputModel() == null) {
            return;
        }


        // populate the table
        ProgCurveOModel progCurveOModel = (ProgCurveOModel) getOutputModel();

        bestOrders_JTable.setModel(new TableModel(progCurveOModel));
        optionsDialog.setOptions(progCurveOModel);
    }

    @Override
    public JDialog getOptionsDialog() {
        return optionsDialog;
    }

    /**
     *
     */
    @Override
    public void enableRuntimeControl() {


        // register to manager if not already so that workers would be notified
        getView().getManager().addWorkerOutputListener(this);


        // spread the Runtime controls:
        // Dynamic Process Log and Dynamic Progress Curve
    }

    /**
     *
     * @param e
     */
    @Override
    public void doProgCurveOutput(WorkerOutputEvent e) {

        final ProgCurve progCurve = (ProgCurve) e.getSource();
        Runnable runnable         = new Runnable() {

            public void run() {


                // update the table
                bestOrders_JTable.setModel(new TableModel(progCurve));

                int top = bestOrders_JTable.getRowCount() - 1;

                if (top > 3) {

                    bestOrders_JTable.scrollRectToVisible(bestOrders_JTable.getCellRect(top, 0,
                            true));
                }


                // update the log
                // update the curve
            }
        };

        SwingUtilities.invokeLater(runnable);
    }

    /**
     * Class description
     *
     * @version        Enter version here..., 12/20/2012
     * @author         Susanta Tewari
     */
    private class TableModel extends AbstractTableModel {

        AnnealOModel[] annealOModels = null;
        final String[] colNames = { "Segment #", "Starting Probe Order", "Best Probe Order",
                                    "Likelihood", "Contig Count" };

        public TableModel(ProgCurveOModel progCurveOModel) {
            annealOModels = progCurveOModel.getAnnealOModelSet().toArray(new AnnealOModel[0]);
        }

        public TableModel(ProgCurve progCurve) {
            annealOModels = progCurve.getAnnealOModelSet().toArray(new AnnealOModel[0]);
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {

            switch (columnIndex) {

                case 0 :
                    return Integer.class;

                case 1 :
                    return String.class;

                case 2 :
                    return String.class;

                case 3 :
                    return Double.class;

                case 4 :
                    return Integer.class;

                default :
                    return String.class;
            }
        }

        @Override
        public int getColumnCount() {
            return 5;
        }

        public int getRowCount() {
            return annealOModels.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {

            switch (columnIndex) {

                case 0 :
                    return rowIndex + 1;

                case 1 :
                    return OrderElement.createOrderString(
                        annealOModels[rowIndex].getInputModel().getOrderSet());

                case 2 :
                    return OrderElement.createOrderString(annealOModels[rowIndex].getOrderSet());

                case 3 :
                    return new Double(annealOModels[rowIndex].getConvValue());

                case 4 :
                    return new Integer(annealOModels[rowIndex].getContigCount());

                default :
                    return null;
            }
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

        public String getColumnName(int columnIndex) {
            return colNames[columnIndex];
        }
    }


    // End of variables declaration//GEN-END:variables
}
